<?php
/**
 * @package common
 *
 */
/**
 * Class which implements several methods which cannot be put 
 * in a better 'more accurate/fitting' place.
 *
 */
class Common {
	/**
	 * Create safe hash for a string which provides more safety then
	 * just a hash which is not randomized
	 * Which means - for the same input text result 
	 * hash is not the same when used twice or more.
	 *
	 * @see Redirector::process
	 * @filesource www.php.net
	 */
	public static function createHash($inText, $saltHash = null, $mode = 'sha1') { 
        /** hash the text */ 
        $textHash = hash($mode, $inText); 
        /** set where salt will appear in hash */ 
        $saltStart = strlen($inText); 
        /** if no salt given create random one */ 
        if ($saltHash == null) { 
            $saltHash = hash($mode, uniqid(rand(), true)); 
        } 
        /** add salt into text hash at pass length position and hash it */ 
        if ($saltStart > 0 && $saltStart < strlen($saltHash)) { 
            $textHashStart = substr($textHash, 0, $saltStart); 
            $textHashEnd = substr($textHash, $saltStart, strlen($saltHash)); 
            $outHash = hash($mode, $textHashEnd.$saltHash.$textHashStart); 
        } elseif ($saltStart >= (strlen($saltHash))) { 
            $outHash = hash($mode, $textHash.$saltHash); 
        } else { 
            $outHash = hash($mode, $saltHash.$textHash); 
        } 
        // put salt at front of hash // 
        $output = $saltHash.$outHash; 
        return $output; 
    }
    
    /**
     * Create hash for a form.
     * This value vould ve expected in Redirector
     * 
     * @see Redirector
     * @return string - new hash
     */
    public static function createFormHash() {
    	return self::createHash(date("Y-m-d").rand(0, 9999999).date("H:i:s"));
    }
    
    /**
     * Create query spec (e.g. clause: "where smth = '??'" )
     * for an array of ids
     * 
     * @param array $arr - array of ids
     * @return string - query specification
     */
    public static function createQuerySpecForIdArray($arr) {
    	$querySpec = "";
    	if (!is_array($arr) || count($arr) <= 0) {
    		throw new ExceptionDisplayable("There are not any items in 'Interested in' box.");
		}	
    	foreach ($arr as $index => $value) {
    		if ($querySpec != "") {
    			$querySpec .= " OR ";
    		} else {
    			$querySpec = " WHERE ";
    		}
    		$querySpec .= "id = '".(int)$index."'";
    	}
    	return $querySpec;
    }
    
    /**
     * Universal way that simply allows you to modify url with more parameters
     * @param string $url - original url (could be just "")
     * @param string $param - parameter to add
     * @param string $value - value of $param
     * @return string - new value of url
     */
    public static function addParamToUrl(string $url, string $param, $value = "") {
    	if ($value == "") {
    		$expr = "$param";
    	} else {
    		$expr = "$param=".urlencode($value);
    	}
    	if ($url == "") {
    		return "?$expr";
    	} else if (preg_match("/\?./", $url)) {
    		return "$url&amp;$expr";
    	} else if (preg_match("/\?$/", $url)) {
    		return "$url$expr";
    	} else {
    		return "$url?$expr";
    	}
    }
    
    /**
     * Add from 1 to 3 parameters to the url (including ther values)
     * @see Common::addParamToUrl
     * @param string $url - Url to add to
     * @param string param1
     * @param string value1
     * @param string param2
     * @param string value2
     * @param string param3
     * @param string value3
     */
    public static function addParamsToUrl(string $url, $param1, $value1 = "", $param2 = null, $value2 = "", $param3 = null, $value3 = "") {
    	$result = $url;
    	for ($i = 1; eval("return isset(\$param$i) && \$param$i !== null;"); ++$i) {
    		$result = self::addParamToUrl($result, eval("return \$param$i;"), eval("return \$value$i;"));
    	}
    	return $result;
    }
    /**
     * This transforms (bijection) filename to an variable identifier
     * Used when uploading images.
     * @param string $fileName - Input value of file name
     * @return string - acceptable identifier's value
     */
    public static function escapeThumbImgToAttr(string $fileName) {
    	$attrValue = preg_replace("/_".Constants::IMG_THUMBMAXSIZE."\.[^\.]+$/", "", $fileName);
		$attrValue = preg_replace("/[\. ]/", "_", $attrValue);
		return "img_" . preg_replace("/^".Constants::IMG_THUMBPREFIX."/", "", $attrValue);
    }
}
?>